using UnityEngine; using UnityEngine.UI; using System.Collections; using EnhancedUI.EnhancedScroller; using EnhancedUI; namespace EnhancedScrollerDemos.SnappingDemo { /// /// This class controls one slot scroller. We could have shared the slot data between the /// three slot controllers, but for demonstration purposes we gave each slot controller their /// own set of data. /// public class SlotController : MonoBehaviour, IEnhancedScrollerDelegate { /// /// This list of slot cells /// private SmallList _data; /// /// The scroller that will display the slot cells /// public EnhancedScroller scroller; /// /// The slot cell view prefab to use in the scroller /// public EnhancedScrollerCellView slotCellViewPrefab; void Awake() { // create a new data list for the slots _data = new SmallList(); } void Start() { // set this controller as the scroller's delegate scroller.Delegate = this; } public void Reload(Sprite[] sprites) { // reset the data list _data.Clear(); // at the sprites from the demo script to this scroller's data cells foreach (var slotSprite in sprites) { _data.Add(new SlotData() { sprite = slotSprite }); } // reload the scroller scroller.ReloadData(); } /// /// This makes the scroller move without having an explicit touch event /// /// public void AddVelocity(float amount) { // set the scroller's linear velocity // (velocity in one direction) scroller.LinearVelocity = amount; } #region EnhancedScroller Callbacks /// /// This callback tells the scroller how many slot cells to expect /// /// The scroller requesting the number of cells /// The number of cells public int GetNumberOfCells(EnhancedScroller scroller) { return _data.Count; } /// /// This callback tells the scroller what size each cell is. /// /// The scroller requesting the cell size /// The index of the data list /// The size of the cell (Height for vertical scrollers, Width for Horizontal scrollers) public float GetCellViewSize(EnhancedScroller scroller, int dataIndex) { return 150f; } /// /// This callback gets the cell to be displayed by the scroller /// /// The scroller requesting the cell /// The index of the data list /// The cell index (This will be different from dataindex if looping is involved) /// The cell to display public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex) { // get the cell view from the scroller, recycling if possible SlotCellView cellView = scroller.GetCellView(slotCellViewPrefab) as SlotCellView; // set the data for the cell cellView.SetData(_data[dataIndex]); // return the cell view to the scroller return cellView; } #endregion } }